package com.hhsjdesign.dynamicui.core.widgetimpl.specwidget

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import com.google.android.material.tabs.TabLayout
import com.hhsjdesign.dynamicui.core.widgetimpl.BusinessWidget
import com.hhsjdesign.dynamicui.R
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import com.hhsjdesign.dynamicui.core.property.widget.MainTabWidgetSpecProperty
import com.hhsjdesign.dynamicui.core.property.ShapeProperty
import com.hhsjdesign.dynamicui.core.property.WidgetSpecProperty
import com.hhsjdesign.dynamicui.utils.BusinessViewCreator
import com.hhsjdesign.dynamicui.utils.BusinessViewCreatorFactory
import com.hhsjdesign.dynamicui.core.property.viewdata.DefaultTabItemViewData
import com.hhsjdesign.dynamicui.utils.ItemViewTypeConst
import org.json.JSONObject

class TabWidget(context: Context, widgetData: WidgetData) : BusinessWidget(context, widgetData) {

    lateinit var tabLayout: TabLayout
    var businessViewCreator: BusinessViewCreator<*>?
    lateinit var floatActionParent: FrameLayout

    init {
        businessViewCreator = BusinessViewCreatorFactory.makeViewHolder("enter");
    }

    override fun createMainView(specProperty: WidgetSpecProperty): View {
        return LayoutInflater.from(context).inflate(R.layout.tab_widget_content_view, null).apply {
            tabLayout = findViewById<TabLayout>(R.id.tab_layout).apply {
                this.isTabIndicatorFullWidth = true
            }
            floatActionParent = findViewById<FrameLayout>(R.id.second_page_tab_layout)
        }
    }

    override fun updateLayoutParam(viewGroup: ViewGroup.LayoutParams) {
    }

    override fun layout(property: ShapeProperty, parentType: String) {
        super.layout(property, parentType)
        contentView?.layoutParams =
            ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, property.height)
    }


    override fun onLoadData() {
        if (widgetData.businessData != null) {
            widgetData.businessData!!.forEach {
                tabLayout.addTab(TabLayout.Tab().apply {
                    this.customView = businessViewCreator?.createView(
                        context,
                        (widgetData.specProperty as MainTabWidgetSpecProperty).itemType.toString(),
                        widgetData.business,
                        DefaultTabItemViewData(ItemViewTypeConst.SERVICE_MENU_1ROW_1COR, JSONObject()),
                        tabLayout
                    )
                    this.tag = it.businessData
                })
            }


        }
    }

    override fun displayBg(image: ImageView, url: String) {
    }

    override fun cancelPlayBg() {
    }

    //添加点击和切换
    fun addOnTabSelectedListener(tabLayoutListener: TabLayout.OnTabSelectedListener) {
        tabLayout.addOnTabSelectedListener(tabLayoutListener)
    }

    //添加事件处理机制
    fun updateFloatActionView(floatView: MutableList<View>) {
        floatActionParent.removeAllViews()
        floatView.forEach { floatActionParent.addView(it) }
    }

    open fun removeAllTab() {
        if (tabLayout.tabCount > 0) {
            tabLayout.removeAllTabs()
            widgetData.businessData = null
        }
    }

}